/*
 * Created by JFormDesigner on Sun Jun 06 19:23:50 CST 2021
 */

package com.J1ay.carsys.gui.Emp.Dialog;

import com.J1ay.carsys.gui.tishi.Success;
import com.J1ay.carsys.gui.tishi.Warning;
import com.J1ay.carsys.pojo.Car;
import com.J1ay.carsys.pojo.Result;
import com.J1ay.carsys.service.EmpService;
import com.J1ay.carsys.service.Impl.EmpServiceImpl;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.GroupLayout;

/**
 * @author Brainrain
 */
public class AddCar extends JDialog {
    int len = 0;
    EmpService empService = new EmpServiceImpl();
    Car car = new Car();
    public AddCar(Window owner, int len) {
        super(owner);
        this.len = len;
        initComponents();
    }
    public void start() {
        this.setVisible(true);
    }
    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        String name = this.name.getText();
        String num = this.num.getText();
        String type = this.type.getText();
        String price = this.price.getText();
        int year = Integer.parseInt(this.year.getValue().toString());
        System.out.println(name + "" +num+ "" +type+ "" +price+ "" +year);
        if (name.equals("") || num.equals("") || type.equals("") || price.equals("") || year <= 0) {
            new Warning(this,"请填写完整信息!").start();
        }
        else {
            car.setName(name);
            car.setNum(num);
            car.setType(type);
            car.setYear(year);
            car.setPrice(price);
            car.setId(Integer.parseInt(String.format("2021%04d", this.len+1)));
            Result result = empService.addCar(car);
            if (200 == result.getStatus()){
                new Success(this,"添加成功!").start();
                this.dispose();
            }
            else{
                new Warning(this,result.getMsg()).start();
            }
        }

    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        name = new JTextField();
        num = new JTextField();
        type = new JTextField();
        year = new JSpinner();
        label6 = new JLabel();
        price = new JTextField();
        button1 = new JButton();

        //======== this ========
        setTitle("\u589e\u52a0\u8f66\u8f86\u4fe1\u606f");
        setResizable(false);
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u6c7d\u8f66\u8f66\u8f86\u4fe1\u606f");

        //---- label2 ----
        label2.setText("\u8f66\u540d");

        //---- label3 ----
        label3.setText("\u8f66\u724c\u53f7");

        //---- label4 ----
        label4.setText("\u8f66\u8f86\u7c7b\u578b");

        //---- label5 ----
        label5.setText("\u8f66\u9f84");

        //---- label6 ----
        label6.setText("\u4ef7\u683c/\u5929");

        //---- button1 ----
        button1.setText("\u6dfb\u52a0");
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                button1ActionPerformed(e);
            }
        });

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(label1, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(427, Short.MAX_VALUE))
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(90, 90, 90)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                        .addComponent(label2, GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE)
                        .addComponent(label3, GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE)
                        .addComponent(label4, GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE)
                        .addComponent(label5, GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                            .addComponent(name, GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE)
                            .addComponent(num, GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE)
                            .addComponent(type, GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addComponent(year, GroupLayout.PREFERRED_SIZE, 119, GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(label6, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(price, GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE)))
                    .addGap(72, 72, 72))
                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                    .addContainerGap(238, Short.MAX_VALUE)
                    .addComponent(button1)
                    .addGap(205, 205, 205))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(label1, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label2, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE)
                        .addComponent(name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label3, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
                        .addComponent(num, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label4, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE)
                        .addComponent(type, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label5, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE)
                        .addComponent(year, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(price, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label6, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addComponent(button1)
                    .addContainerGap(22, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JTextField name;
    private JTextField num;
    private JTextField type;
    private JSpinner year;
    private JLabel label6;
    private JTextField price;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
